package cn.hollis.nft.turbo.chain.domain.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.hollis.nft.turbo.api.chain.constant.ChainType;
import cn.hollis.nft.turbo.base.utils.BeanNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author liuyjy
 * @Date 2025/2/13 11:07
 * @Description: 策略工厂
 * @Version 1.0
 * https://www.cnblogs.com/jelly12345/p/17799614.html
 **/
@Service
public class ChainServiceFactory {

    /**
     * 自动装配
     * 缓存所有Service
     */
    @Autowired
    private final Map<String, Service> serviceMap = new ConcurrentHashMap<String, ChainService>();

    public Service get(ChainType chainType) {
        String beanName = BeanNameUtils.getBeanName(chainType.name(), "Service");

        //组装出beanName，并从map中获取对应的bean
        Service service = serviceMap.get(beanName);

        if (service != null) {
            return service;
        } else {
            throw new UnsupportedOperationException(
                    "No ChainService Found With chainType : " + chainType);
        }
    }
}
